/* @(#)func.c
 * Time-stamp: <2008-10-25 20:10:12 root>
 * Copyright 2008 Julian Qian
 * Author: root@localhost.localdomain
 * Version: $Id: func.c,v 0.0 2008/09/27 16:44:50 root Exp $
 * Keywords: 
 */

#include "common.h"

char * trim(char* dstr){
	int n, nLen;
	if (dstr != NULL)	{
		for (n = strlen(dstr); n > 0; n--)		{
			if (!isblank(dstr[n - 1]))  break;
		}
		dstr[n] = '\0';

		nLen = strlen(dstr);
		for (n = 0; n < nLen; n++) {
			if (!isblank(dstr[n])) break;
		}
		strcpy(dstr, dstr + n);
	}
	return dstr;
}

/* devid[12] -> devid_f[24], convert into an ASCII string*/
void cvt_devid(char *devid, char *devid_f){
	int i;
	char chr;
	for(i=0;i<DEVID_NUM_ALL;i++){
		chr = devid[i] >> 4;
		chr = chr & 0x0F;
		devid_f[2*i] = ((chr > 0x09)?(chr-0x0A+'A'):(chr+'0'));
		chr = devid[i] & 0x0F;
		devid_f[2*i+1] = ((chr > 0x09)?(chr-0x0A+'A'):(chr+'0'));
	}	
}

void cvt_iddev(char *devid_f, char *devid){
	int i;
	char chr, chs;
	for(i=0; i<DEVID_NUM_ALL; i++){
		chr = devid_f[2*i];
		chr = (chr>'9' ?  chr-'A'+0x0A: chr-'0');
		chs = (chr << 4) & 0xF0;
		
		chr = devid_f[2*i+1];
		chr = (chr>'9' ?  chr-'A'+0x0A: chr-'0');		
		chs = chs | (chr & 0x0F);
		devid[i] = chs;
	}
}

/*
devid_t cvt_id(char *devid){
	devid_t id = 0;
	memcpy(&id, devid, DEVID_NUM);
	id = id >> (8-DEVID_NUM);
	return id;
}
*/

devid_t cvt_id(char *devid){
	int i;
	devid_t id = 0;
	for(i=0;i<DEVID_NUM;i++){ /* DEVID_NUM = 5 */
		id = id << 8;
		id += devid[i];
	}
	return id;
}

void cvt_di(devid_t id, char *chrid){
	char chr;
	int i;
	for (i=0; i<DEVID_NUM; i++) {
		chr = id >> 8*i;
		*(chrid+DEVID_NUM-i-1) = chr;
	}
}

/* convert ip annoation into a intger */
ip_t cvt_ip(char *ip_str){
	char *pch;
	ip_t sub, ip = 0;
	pch = strtok(ip_str, ".");
	while(pch != NULL){
		sub = atoi(pch);
		ip = ip * 256 + sub;
		pch = strtok( NULL, ".");
	}
	return ip;
}

char *IP(ip_t ip){
	struct in_addr ia;
	ia.s_addr = htonl(ip);
	return inet_ntoa(ia);
}

/* convert an intger into an IP anonation string */
void cvt_pi(ip_t ip, char *ip_str){
	int a, b, c, d;
	d = ip & 0x000000FF;
	c = (ip >> 8) & 0x000000FF;
	b = (ip >> 16) & 0x000000FF;
	a = (ip >> 24) & 0x000000FF;
	snprintf(ip_str, 16, "%d.%d.%d.%d",a,b,c,d);
}


void print_hex(char *hex, int len){
	int i;
	printf("(%d)-------------->\n", len);
	for (i=0; i<len; i++) {
		printf("%#x ",hex[i]);
	}
	printf("\n");
}

int get_host_addr(char *ip_addr){
	char hostname[MAXLEN];
	if(gethostname(hostname, sizeof(hostname)) == -1){
		perror("gethostname");
		return -1;
	}else{
		struct hostent *h;
		if((h = gethostbyname(hostname)) == NULL){ /* get address */
			herror("gethostbyname");
			return -1;
		}
		strncpy(ip_addr, inet_ntoa(*((struct in_addr *)h->h_addr)), MAXLEN);
	}
	return 0;
}

stat_t get_stat(char *buff){
	stat_t stat = STAT_NULLL;
	switch(buff[PROTO_TYPE]){
	case CLI_RESET:
		stat = STAT_RESET;
		break;
	case CLI_ALARM:
	case CLI_TIMER:
	case CLI_DEFEN:
	case CLI_UPDATE:
		/* stat = STAT_TIMER; */
		switch(buff[PROTO_DATA]){
		case CTRL_DEFEN:
			stat = STAT_DEFEN; break;
		case CTRL_UNDEF:
			stat = STAT_UNDEF; break;
		case ALARM_MANUA:
			stat = STAT_ALARM; break;
		case ALARM_DOORR:
			stat = STAT_DOORR; break;
		case ALARM_INFRA:
			stat = STAT_INFRA; break;
		}
		break;
	default:
		stat = STAT_OTHER;
		break;
	}
	return stat;
}

void set_stat(char *buff, char type, char data){
	buff[PROTO_TYPE] = type;
	buff[PROTO_DATA] = data;
}

spec_t get_spec(char *buff){
	switch(buff[PROTO_SPEC]){
	case SPEC1:
		return SPEC_1;
	case SPEC2:
		return SPEC_2;
	default:
		return SPEC_0;
	}
}

devid_t get_devid(char *buff){
	return cvt_id( buff + PROTO_DEVI);
}

int gen_shm_device(){
	int fd;
	fd = open(SHM_DEVICE, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
	if(-1 == fd) return -1;
	close(fd); return 0;
}
